{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Layering text over a heatmap\n",
    "----------------------------\n",
    "\n",
    "An example of a layered chart of text over a heatmap using the cars dataset.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import altair as alt\n",
    "alt.data_transformers.enable('json')\n",
    "from altair.expr import datum\n",
    "from vega_datasets import data\n",
    "\n",
    "cars = data.cars.url\n",
    "\n",
    "heatmap = alt.Chart(cars).mark_rect().encode(\n",
    "    alt.X('Cylinders:O', scale=alt.Scale(paddingInner=0)),\n",
    "    alt.Y('Origin:O', scale=alt.Scale(paddingInner=0)),\n",
    "    color='count()'\n",
    ")\n",
    "\n",
    "text = alt.Chart(cars).mark_text(baseline='middle').encode(\n",
    "    x='Cylinders:O',\n",
    "    y='Origin:O',\n",
    "    text='count()',\n",
    "    color=alt.condition(datum['count_*'] > 100,\n",
    "                        alt.value('black'),\n",
    "                        alt.value('white'))\n",
    ")\n",
    "\n",
    "heatmap + text\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "env": {},
   "interrupt_mode": "signal",
   "language": "python",
   "metadata": {},
   "name": "python3"
  },
  "language": "python"
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
